//
//  JoinUsTableViewController.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/7/18.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class JoinUsTableViewController: UITableViewController, HeardDelegate {

    /// 当前频道
    var channel: String!
    /// 当前频道的url
    var channelUrl: String!
    var page = 0
    var selectedPage = 0
    var modelList: [JoinUsModel]? {
        didSet {
            self.tableView.reloadData()
        }
    }
    var hasSelected: Bool = false
    var newsTableHeadViewController = NewsHeadViewController()
    var fontSize: CGFloat?
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(JoinUsTableViewController.requestInfo))
        
        self.tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(JoinUsTableViewController.nextRequestInfo))
        
        self.tableView.mj_header.beginRefreshing()
        
        self.newsTableHeadViewController.url = self.channelUrl
        
        self.addChildViewController(self.newsTableHeadViewController)
        self.tableView.tableHeaderView = self.newsTableHeadViewController.view
        
        self.newsTableHeadViewController.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        if ApplicationDelegate.isLogin(){
            let phone = publicMethod.userName()
            let pass = publicMethod.passWord()
            let token = publicMethod.token()
            DataTool.login(phone, pass: pass, token: token, completionHandler: { (newModel) in
                guard let newDataes = newModel else {
                    return
                }
                print(newDataes)
                
            })
            
        }
        
        let str = ApplicationDelegate.fontSize()
        self.fontSize  =  CGFloat ( (str as NSString).floatValue)
        self.tabBarController?.tabBar.hidden = false
    }
    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if let count = self.modelList?.count {
            return count
        }
        return 0
    }
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let model = self.modelList![indexPath.row]
        if model.type == "求职"{
            return 120
        }else{
            return 93
        }
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let model = self.modelList![indexPath.row]
        
        if model.type == "求职" {
            let cell = tableView.dequeueReusableCellWithIdentifier("JoinUsOfPersonTableViewCell", forIndexPath: indexPath) as! JoinUsOfPersonTableViewCell
            
            
           // cell.title.text = "\(model.type) \(model.unittype) \(model.positiontype)"
            cell.title.text = "\(model.type) \(model.positiontype)"
            cell.title.font = UIFont.systemFontOfSize(self.fontSize!)
            cell.sex.text = model.sex
            cell.age.text = String(model.age)
            cell.name.text = model.name
            cell.address.text = model.unitarea
            cell.years.text = model.lift
            cell.icon.sd_setImageWithURL(NSURL(string: model.imgsrc), placeholderImage: UIImage(named: "placeholder"))
            cell.money.text = model.pay
            
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("JoinUsOfCompanyTableViewCell", forIndexPath: indexPath) as! JoinUsOfCompanyTableViewCell
            
            // Configure the cell...
            cell.title.text = model.title
            cell.title.font = UIFont.systemFontOfSize(self.fontSize!)
            cell.address.text = "\(model.unitarea)"
            cell.money.text = model.pay
            cell.years.text = model.lift
            cell.auth.text = model.unittag
            if model.unittag == "执照已认证"{
                cell.auth.layer.masksToBounds = true
                cell.auth.layer.borderWidth = 1
                cell.auth.layer.cornerRadius = 10
            }
            if model.unittag == "金牌企业"{
                cell.auth.backgroundColor = UIColor.yellowColor()
            }
            return cell
        }
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let model = self.modelList![indexPath.row]
        
        if model.type == "求职" {
            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("JoinWantedDetailVC") as! JoinWantedDetailVC
            vc.id = model.id
            vc.type = model.type
            self.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
            self.hidesBottomBarWhenPushed = false
            }else{
            
            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("JoinUsRecruitDetailVC") as! JoinUsRecruitDetailVC
            vc.id = model.id
            vc.type = model.type
            self.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
            self.hidesBottomBarWhenPushed = false
        }
    }

    
    // MARK: - Navigation

    
    
    /**
     下拉刷新，加载最新数据
     */
    func requestInfo() {
        self.page = 0
        if self.hasSelected == false{
           
            let urlStr = String(format: "%@infomationjson/\(self.channelUrl)",BaseUrl)
            DataTool.loadJoinUsData(urlStr,completionHandler: { (newsArray) -> Void in
                self.tableView.mj_header.endRefreshing()
                guard let newDataes = newsArray else {
                    return
                }
                self.modelList = newDataes
            })
        }else if self.hasSelected == true{
            self.selectedPage = 0
            DataTool.dropMenuScreen_qzzp(self.newsTableHeadViewController.position!, unitarea: self.newsTableHeadViewController.unitarea!, positiontype:self.newsTableHeadViewController.positiontype! , pay: self.newsTableHeadViewController.pay!, page:String(self.selectedPage) ,completionHandler: { (newsArray) -> Void in
                self.tableView.mj_header.endRefreshing()
                guard let newDataes = newsArray else {
                    return
                }
                print(newDataes)
                self.modelList = newDataes
            })

        }
        
       
    }
    
    /**
     上拉刷新，加载最新数据
     */
    func nextRequestInfo(){
        if self.hasSelected == false{
            self.page += 1
            let url = String(format: "%@jobrecruitment/dosel/fenye/\(self.page)",BaseUrl)
            DataTool.loadNextJoinUsEx(String(self.page), urlStr: url, completionHandler: { (newsArray) -> Void in
                self.tableView.mj_footer.endRefreshing()
                guard let newDataes = newsArray else {
                    return
                }
                self.modelList! += newDataes
            })
        }else{
            self.selectedPage += 1
            DataTool.dropMenuScreen_qzzp(self.newsTableHeadViewController.position!, unitarea: self.newsTableHeadViewController.unitarea!, positiontype:self.newsTableHeadViewController.positiontype! , pay: self.newsTableHeadViewController.pay!,page:String(self.selectedPage), completionHandler: { (newsArray) -> Void in
                self.tableView.mj_footer.endRefreshing()
                guard let newDataes = newsArray else {
                    return
                }
                print(newDataes)
                self.modelList! += newDataes
            })

        }
        
    }
    
   
    // MARK: - HeardDelegate
    func onHeightChange(newView: UIView) {
        self.tableView.tableHeaderView = newView
    }
    func setContentViewControllerWithInfo(cellInfo: [NewsModel]? ) -> Void{
       
    }
    func setContentTransferViewControllerWithInfo(cellInfo: [TransferModel]?) -> Void{
        
    }
    func setContentJoninUsViewControllerWithInfo(cellInfo: [JoinUsModel]?) -> Void{
        self.modelList = cellInfo
        self.hasSelected = true
        
    }
    func setContentMXQYViewConreollerWithInfo(cellInfo: [starBusinessInfo]?) -> Void{
        
    }
    func setContentZYCPViewControllerWithInfo(cellInfo: [specialProductInfo]?) -> Void{
        
    }
    
    func CyclePictureViewSelected(){

    }
    func onSendInfoClick() -> Void {
        
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("JoinUsPublishMessageVC") as! JoinUsPublishMessageVC
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
        self.hidesBottomBarWhenPushed = false
    }
}
